package com.dc.service.Impl;

import com.dc.dao.IpLogRepository;
import com.dc.po.IpLog;
import com.dc.service.IpLogService;
import com.dc.vo.IpCount;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author xingzq
 * @version 1.0
 * @date 2022/2/15 11:29
 */
@Service
public class IpLogServiceImpl implements IpLogService {

    @Autowired
    private IpLogRepository ipLogRepository;

    @Override
    public IpLog saveIpLog(IpLog ipLog) {
        return ipLogRepository.save(ipLog);
    }

    @Override
    public void deleteIpLog(Long id) {
        ipLogRepository.deleteById(id);
    }

    @Override
    public Page<IpLog> listIpLog(Pageable pageable) {
        return ipLogRepository.findAll(pageable);
    }

    @Override
    public Long countByIp(String ip) {
        return ipLogRepository.countAllByIp(ip);
    }

    @Override
    public List<IpCount> countIpByGroup() {
        List<IpCount> list = new ArrayList<>();
        List _list = ipLogRepository.countIp1();
        for(Object row:_list){
            Object[] cells = (Object[]) row;
            IpCount ipCount = new IpCount();
            ipCount.setIp(cells[0].toString());
            ipCount.setTotal(cells[1].toString());
            if(cells[3]==null){
                ipCount.setArgs((String) cells[2]);
            }else {
                ipCount.setArgs((String) cells[3]);
            }
            ipCount.setDate((Date) cells[4]);
            list.add(ipCount);
        }
        return list;
    }

}
